﻿using System.Collections.Generic;
using System.Linq;

namespace PassThePuzzle
{
    public static class Utils
    {
        public static string nbsp = "\u00a0";

        /// <summary>
        /// Swap 2 values
        /// </summary>
        /// <param name="a">first value</param>
        /// <param name="b">second value</param>
        public static void Swap<T>(ref T a, ref T b)
        {
            T tmp = a; a = b; b = tmp;
        }
        /// <summary>
        /// Swap 2 integers if first integer is larger than second integer
        /// </summary>
        /// <param name="a">first integer</param>
        /// <param name="b">second integer</param>
        public static void SwapIfNeeded(ref int a, ref int b)
        {
            if (a > b) { Swap(ref a, ref b); }
        }

        /// <summary>
        /// Returns all permutations of the input IEnumerable&lt;T&gt;.
        /// </summary>
        /// <param name="items">The list of items to permute.</param>
        /// <returns>IEnumerable&lt;IEnumerable&lt;T&gt;&gt; containing all permutations of the input IEnumerable&lt;T&gt;.</returns>
        public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> items)
        {
            var c = items.Count();
            if (c == 1)
                yield return items;
            else
                for (int i = 0; i < c; i++)
                    foreach (var p in Permutations(items.Take(i).Concat(items.Skip(i + 1))))
                        yield return items.Skip(i).Take(1).Concat(p);
        }

        /// <summary>
        /// Returns count, a space and the appropriate word
        /// </summary>
        /// <param name="count">number</param>
        /// <param name="singular">singular word e.g. ox</param>
        /// <param name="plural">plural word e.g. oxen (if omitted, assumes add "s"/"es" to singular)</param>
        /// <returns></returns>
        public static string Pluralise(int count, string singular)
        {
            return Pluralise(count, singular, singular + (singular[singular.Length - 1] == 's' ? "es" : "s"));
        }
        public static string Pluralise(int count, string singular, string plural)
        {
            return (count + " " + (count == 1 ? singular : plural));
        }
    }
}
